import axios from 'axios'
import tools from './tools'
import comm from "./comm"
import ui from "./ui"
import router from '../router/index';
export default {
    isReqAccessToken: false,
    cbList: [],
    /**
     * 
     * @param {*} method post/get
     * @param {*} parma 请求参数(callback.methodName callback.data)
     */
    ajax: function (callback, reqType) {
        if (!reqType) {
            reqType = "post";
        }
        let tokenString = tools.getLocalData(comm.TOKEN);
        let token = tokenString != "undefined" ? tokenString : null;

        let auth = "Bearer " + JSON.parse(token);
        if (!callback.url) {
            callback.url = comm.apiUrl();
        }

        axios({
            method: reqType,
            headers: { authorization: auth },
            url: callback.url + "/" + callback.methodName,
            data: tools.crypto.Encrypt(JSON.stringify(callback.data)) //请求参数加密
        })
            .then(res => {
                var data = res.data;
                if (data.code == 0) {
                    //失败原因提示
                    ui.dialog({
                        msg: data.des
                    });
                    callback.s(0, data);
                } else if (data.code == -10) {
                    //accessToken 过期情况，则刷新accessToken
                    this.getAccessTokenByRefreshToken(callback, reqType);
                } else if (data.code == -20) {
                    //refreshToken 过期，则需重新登录
                    this.isReqAccessToken = false;
                    ui.dialog({
                        msg: data.des,
                        type: "warning",
                        close() {
                            router.push('/login');
                        }
                    });
                } else {
                    callback.s(1, data);
                }
            })
            .catch(function (error) {
                ui.dialog({
                    msg: error
                });
                callback.s(-1, error);
            });
    },
    async getAccessTokenByRefreshToken(callback, reqType) {
        this.cbList.push(callback);
        if (this.isReqAccessToken) {
            this.isReqAccessToken = false;
            return;
        }
        this.isReqAccessToken = true;

        let userInfo = JSON.parse(tools.getLocalData(comm.USERINFO));
        let refreshToken = "";
        if (userInfo && userInfo.token) {
            refreshToken = userInfo.token.refreshToken;
        }
        let res = await this.getAccessToken(refreshToken);
        this.isReqAccessToken = false;
        if (res.code == 1) {
            //更新本地accessToken信息
            userInfo.token = res.data;
            tools.setLocalData(comm.USERINFO, JSON.stringify(userInfo));
            this.cbList.forEach(cb => {
                this.ajax(cb);
            });
        }
        this.cbList = [];
    },
    getAccessToken(refreshToken) {
        return new Promise((resolve, reject) => {
            this.ajax({
                methodName: 'user/getAccessToken',
                data: {
                    refreshToken: refreshToken
                },
                s(code, res) {
                    if (code == 1) {
                        resolve(res);
                    } else {
                        reject(res);
                    }
                }
            });
        });
    },
}